import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'
import store from '@/store'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  routes
})

// 全局前置守卫
// 判断用户是否登录
router.beforeEach((to, from, next) => {
  const token = store.state.token

  console.log('token:', token)

  if (token) {
    // 存在token，则说明已登录
    next()
  } else {
    // 不存在token，则跳转到登录页面
    next(false)
    window.location.href = '/login.html'
  }
})

export default router
